#include "Udp_server.hpp"
#include<memory>
#include<cstdio>

using namespace std;
using namespace ydd_srver;
static void usage(string proc)
{
    cout<<"Usage\n\t"<<proc<<" port\n"<<endl;
}

static bool isPass(const std::string &command)
{   
    bool pass = true;
    auto pos = command.find("rm");
    if(pos != std::string::npos) pass=false;
    pos = command.find("mv");
    if(pos != std::string::npos) pass=false;
    pos = command.find("while");
    if(pos != std::string::npos) pass=false;
    pos = command.find("kill");
    if(pos != std::string::npos) pass=false;
    return pass;
}

std::string excuteCommand(std::string command) // command就是一个命名
{
    // 1. 安全检查
    if(!isPass(command)) return "you are bad man!";

    // 2. 业务逻辑处理
    FILE *fp = popen(command.c_str(), "r");//popen函数执行指令，并将执行结果放入文件中并返回
    if(fp == nullptr) return "None";
    // 3. 获取结果了
    char line[1024];
    std::string result;
    while(fgets(line, sizeof(line), fp) != NULL)
    {
        result += line;
    }
    pclose(fp);

    return result;
}


int main(int argc,char *argv[])
{
    if(argc !=2 )
    {
        usage(argv[0]);
        exit(USAGE_ERR);
    }

    uint16_t port = atoi(argv[1]);
    unique_ptr<UdpServer> user(new UdpServer(port));
    user->start();
    //user->Recv();

    return 0;
}

//./Udp_server port
// int main(int argc,char *argv[])
// {
//     if(argc !=2 )
//     {
//         usage(argv[0]);
//         exit(USAGE_ERR);
//     }

//     uint16_t port = atoi(argv[1]);
//     unique_ptr<UdpServer> user(new UdpServer(excuteCommand,port));
//     user->start();
//     user->Recv();

//     return 0;
// }